<?php

require_once '../MMysql.php';
require_once("../../mobile/SendTemplateSMS.php");
require_once("../../phpmailer/send.php");

/**
 * Created by PhpStorm.
 * User: hcadmin
 * Date: 16/8/18
 * Time: 上午9:17
 */
class likefuction
{
    public function addlikes($like_fullname,$like_email,$like_tel,$like_wechat,$like_address,$property_id)
    {

        $mysql = new MMysql();
        $flag= $mysql->insert('likes',array(
            'like_fullname'=>$like_fullname,
            'like_email'=>$like_email,
            'like_tel'=>$like_tel,
            'like_wechat'=>$like_wechat,
            'like_address'=>$like_address,
            'del_flag'=>0,
            'create_time'=>date('y-m-d',time()),
            'property_id'=>$property_id,
        ));

        if($flag==1){

            //查找该房子的代理商邮箱
            $property = $mysql
                ->where(array('property_id'=>$property_id,'del_flag'=>0))
                ->select('property');

            if(count($property)>0){

                $agent = $mysql
                    ->where(array('agent_id'=>$property[0]['property_agent_id'],"del_flag"=>0))
                    ->select('agent');

                $account="admin";
                $admin= $mysql
//                    ->where(array("account"=>$account,"del_flag"=>0))
                    ->select('user');
//
//                echo $admin;

                if(count($agent)>0){
                    //邮箱通知
                    sendMail($admin[0]['user_email'],'DMI','投资人'.$like_fullname.'喜欢了你负责的澳洲房产“'.$property[0]['property_name'].'”
<br>投资人邮箱：'.$like_email.'
<br>投资人联系电话：'.$like_tel.'
<br>投资人微信：'.$like_wechat.'
<br>投资人目前住址：'.$like_address.'
<br>请您尽快进入管理端查看吧。点击链接进入网站：www.dymondmynes.com');

                    //手机通知
                    sendTemplateSMS($admin[0]['user_tel'],array($like_fullname,$property[0]['property_name']),"120196");

                    sendMail($agent[0]['agent_email'],'DMI','投资人'.$like_fullname.'喜欢了你负责的澳洲房产“'.$property[0]['property_name'].'”
<br>投资人邮箱：'.$like_email.'
<br>投资人联系电话：'.$like_tel.'
<br>投资人微信：'.$like_wechat.'
<br>投资人目前住址：'.$like_address.'
<br>请您尽快进入管理端查看吧。点击链接进入网站：www.dymondmynes.com');

                    //手机通知
                    sendTemplateSMS($agent[0]['agent_tel'],array($like_fullname,$property[0]['property_name']),"120196");

                    echo $flag;
                }

            }

        }
        else{
            echo 0;
        }
    }

    public function selectlikes($property_id,$page,$rows)
    {

        $mysql = new MMysql();
        $res = $mysql
            ->where(array('property_id'=>$property_id,"del_flag"=>0))
            ->order(array('like_id'=>'desc'))
            ->limit($page,$rows)
            ->select('likes');
       
        $arraya=Array();
        foreach ($res as $u){

            //代理商
            $property_name=$mysql->where(array('property_id'=> $u["property_id"]))->select('property');
            $u["property_name"]=$property_name[0]['property_name'];

            $arraya[]=$u;
        }

        $count=$mysql->where(array('property_id'=>$property_id,"del_flag"=>0))->count('likes');

        $array=Array();
        $array["rows"]=$arraya;
        $array["total"]=$count;
        echo json_encode($array);
    }
}


$method =$_REQUEST["method"];

if($method=='list'){

    $d=new likefuction();

    return $d->selectlikes($_REQUEST["property_id"],$_REQUEST["page"],$_REQUEST["rows"]);

}
else if($method=='add'){
    $d=new likefuction();

//    echo $_REQUEST["parameter_value"];

    echo $d->addlikes(
        $_REQUEST["like_fullname"],$_REQUEST["like_email"],$_REQUEST["like_tel"],
        $_REQUEST["like_wechat"],$_REQUEST["like_address"],$_REQUEST["property_id"]);
}

